package br.com.classificadosonline.persistencia;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import br.com.classificadosonline.nucleo.Login;

public class LoginDAO {

	private static final LoginDAO instance = null;

	public static LoginDAO getInstance()
	{
		if (instance == null)
		{
			instance = new LoginDAO();
		}
		return instance;
	}

	public static LoginDAO obterInstancia() {
		return (LoginDAO) Conexao.select("SELECT * FROM LOGIN");
	}
	
	public static void cadastrarLogin(String usuario, String senha) {
		Conexao.update("INSERT INTO LOGIN VALUES ('" + usuario + "', '" + senha + "')");
	}
	
	public static void modificarLogin(String usuario, String senha) {
		Conexao.update("UPDATE LOGIN SET SENHA='" + senha + "' WHERE USUARIO='" + usuario + "'");
	}	

	public static Login buscarLogin(String usuario) {
		return buscar("SELECT * FROM LOGIN WHERE USUARIO='" + usuario + "'").get(0);
	}
	
	public static List<Login> buscar(String query) {
		ResultSet rs = null;
		List<Login> lista = new ArrayList<Login>();
		Login login = null;	
		Statement stmt;
		try {
			stmt = Conexao.conexao().createStatement();							
			rs = stmt.executeQuery(query);
			while (rs.next()) {
				login = new Login(rs.getString("USUARIO"), rs.getString("SENHA"));
				lista.add(login);
			}
			stmt.close();
			Conexao.conexao().close();	
		} catch (SQLException e) {
		}
		return lista;
	}
	
	public static List<Login> listarLogin() {
		return buscar("SELECT * FROM LOGIN");
	}
	
	public static void excluirLogin(String usuario) {
		Conexao.update("DELETE FROM LOGIN WHERE USUARIO='" + usuario + "'");
	}
}
